package com.atguigu.imperial.court.servlet.module;

import com.atguigu.imperial.court.entity.Memorials;
import com.atguigu.imperial.court.service.api.MemorialsService;
import com.atguigu.imperial.court.service.impl.MemorialsServiceImpl;
import com.atguigu.imperial.court.servlet.base.ModelBaseServlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class WorkServlet extends ModelBaseServlet {
    private MemorialsService memorialsService=new MemorialsServiceImpl();

    protected void showMemorialsDigestList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Memorials> memorialsList=memorialsService.getAllMemorialsDigest();
        request.setAttribute("memorialsList",memorialsList);
        String templateName="memorials-list";
        processTemplate(templateName,request,response);
    }

    protected void showMemorialsDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.从请求参数获取 memorialsid
        String memorialsId=request.getParameter("memorialsId");
        //2.根据 memorialsId 从 Service 中查询 memorials 对象
        Memorials memorials=memorialsService.getMemorialsDetailById(memorialsId);
        /*************补充功能 begin **************/
        //获取当前奏折的状态
        long memorialsStatus = memorials.getMemorialsStatus();
        //判断奏折状态
        if(memorialsStatus==0){
            memorialsService.updateMemorialsStatusToRead(memorialsId);
            memorials.setMemorialsStatus(1);
        }
        /*************补充功能 end **************/
        //3.将 Memorials 对象存入请求域
        request.setAttribute("memorials",memorials);
        //4.解析渲染视图
        String templateName="memorials_detail";
        processTemplate(templateName,request,response);
    }

    protected void feedBack(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取表单提交的请求
        String memorialsId=request.getParameter("memorialsId");
        String feedbackContent=request.getParameter("feedbackContent");
        //执行更新
        memorialsService.updateMemorialsFeedBack(memorialsId,feedbackContent);
        //重定向回显奏折页面
        response.sendRedirect(request.getContextPath()+"/work?method=showMemorialsDigestList");
    }
}
